﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Text;

namespace HQWeb.LibarySys
{
    public partial class PurchaseInLibrary : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindBranchName();
                if (Request["sign"] == "1")
                {
                    apply.Style["display"] = "none";
                    string strID = Request["id"];
                    BindData(strID);
                }
                else
                {
                    applicant.Value = Session["USER_NAME"].ToString();
                    applicant.Disabled = true;
                }
                if (Request["mid"] != null)
                {
                    BindMaterialType(int.Parse(Request["mid"]));
                }
            }
        }

        protected void BindMaterialType(int id)
        {
            Model.SysManage.T_PUB_MATERIAL model = new Model.SysManage.T_PUB_MATERIAL();
            BLL.SysManage.T_PUB_MATERIAL bll = new BLL.SysManage.T_PUB_MATERIAL();
            model = bll.GetModel(id);
            hidTypeID.Value = model.TYPE_ID.ToString();
        }

        #region 绑定分店列表
        private void BindBranchName()
        {
            BLL.SysManage.T_PUB_BRANCH LibraryBll = new BLL.SysManage.T_PUB_BRANCH();
            IList il = LibraryBll.GetModelList("");
            drop_branch.DataSource = il;
            drop_branch.DataTextField = "BRANCH_NAME"; //dropdownlist的Text的字段 
            drop_branch.DataValueField = "BRANCH_ID";//dropdownlist的Value的字段 
            drop_branch.DataBind();
            ListItem item = new ListItem("", "");
            item.Selected = true;
            drop_branch.Items.Add(item);
        }
        #endregion

        #region 绑定仓库
        private void BindLibraryName(int Branch_id)
        {
            BLL.SysManage.T_PUB_LIBRARY bll = new BLL.SysManage.T_PUB_LIBRARY();
            IList librarylist = bll.GetModelByBranch_id(Branch_id);
            if (librarylist != null)
            {
                droplibrary.DataSource = librarylist;
                droplibrary.DataTextField = "LIBRARY_NAME";
                droplibrary.DataValueField = "LIBRARY_ID";
                droplibrary.DataBind();
            }
            drop_branch.Disabled = true;
            //drop_branch.Style["readonly"] = "readonly";
            droplibrary.Disabled = true;
            //droplibrary.Style["readonly"] = "readonly";
        }
        #endregion

        protected void BindData(string strID)
        {
            Model.Procurement.T_PUB_WAREHOUSING model = new Model.Procurement.T_PUB_WAREHOUSING();
            BLL.Procurement.T_PUB_WAREHOUSING bll = new BLL.Procurement.T_PUB_WAREHOUSING();
            model = bll.GetModel(strID);
            applytitel.Value = model.TITEL;
            drop_branch.Value = model.BRANCH_ID.ToString();
            BindLibraryName((int)model.BRANCH_ID);
            droplibrary.Value = model.LIBRARY_ID.ToString();
            applicant.Value = model.APPLICANT_NAME;
            Remark.Value = model.REMARK;
            BindMaterial(strID);
            applytitel.Disabled = true;
            Remark.Disabled = true;
            applicant.Disabled = true;
            //applytitel.Style["readonly"] = "readonly";
            //Remark.Style["readonly"] = "readonly";
            //applicant.Style["readonly"] = "readonly";
        }
        #region 绑定材料
        private void BindMaterial(string id)
        {
            BLL.Procurement.T_PUB_PURCHASEMATERIAL bll = new BLL.Procurement.T_PUB_PURCHASEMATERIAL();
            DataTable modelList = bll.GetListByPid(id).Tables[0];
            StringBuilder strHtml = new StringBuilder();
            int count = modelList.Rows.Count;
            string strEID;
            string strMateral;
            string strMateralName;
            string strNum;

            for (int i = 0; i < count; i++)
            {
                strEID = modelList.Rows[i]["ID"].ToString();
                strMateral = modelList.Rows[i]["MATERIAL_ID"].ToString();
                strMateralName = modelList.Rows[i]["MATERIAL_NAME"].ToString();
                strNum = modelList.Rows[i]["NUM"].ToString();

                strHtml.Append("  <table width=\"58%\" id=\"table" + i + "\">");
                strHtml.Append(" <tr>");
                strHtml.Append("<td align=\"right\" style=\"width:10%\">");
                strHtml.Append("元素名称：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width:10%\">");
                strHtml.Append(" <input type=\"text\" readonly=\"readonly\" id=\"n" + i + "\" onchange=\"this.setAttribute('value',this.value);\" value=\"" + strMateralName + "\"/>");
                strHtml.Append("</td>");
                strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                strHtml.Append("申请数量：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width: 10%\">");
                strHtml.Append("<input type=\"text\" readonly=\"readonly\" onchange=\"this.setAttribute('value',this.value);\" id=\"s" + i + "\" value=\"" + strNum + "\"/>");
                strHtml.Append(" </td>");
                //strHtml.Append("<td style=\"width: 10%\" align=\"right\">");
                //strHtml.Append("库存数量：");
                //strHtml.Append("</td>");
                //strHtml.Append("<td align=\"left\">");
                //strHtml.Append("<input type=\"text\" onchange=\"this.setAttribute('value',this.value);\" readonly=\"readonly\" id=\"c" + i + "\" value=\"" + strNum + "\"/>");
                //strHtml.Append(" </td>");
                //strHtml.Append("<td align=\"right\" style=\"width: 10%\" >");
                //strHtml.Append("<input type=\"button\" id=\"del" + i + "\" value=\"删除\"  onclick=\"DeleteNode('table" + i + "')\"/>");
                //strHtml.Append("</td>");
                strHtml.Append(" <input type=\"text\" id=\"p" + i + "\" style=\"display: none\" value=\"" + strMateral + "\"/>");
                strHtml.Append(" <input type=\"text\" id=\"e" + i + "\" style=\"display: none\" value=\"" + strEID + "\" />");
                strHtml.Append(" </tr>");
                strHtml.Append(" </table>");
            }
            Addtable.InnerHtml = strHtml.ToString();
            hidCount.Value = count.ToString();
        }
        #endregion

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            if (Request["sign"] == "0" || Request["sign"] == "1")
            {
                Response.Redirect("PurchaseInList.aspx");
            }
            else
            {
                Response.Redirect("../Procurement/InventoryList.aspx?pid=" + hidTypeID.Value);
            }
        }
    }
}